var audio = $('audio')[0];
$(function () {
    load();
});
function load(currentPage) {
    var mlid = getParameter("mlid");
    console.log("mlid:"+mlid);
    $.get("/music/music/findMusicInListByPage",{currentPage:currentPage,mlid:mlid},function (data) {
        console.log(data);
        if (data.list!=null){
            //获取tbody
            $list = $("#list");
            //拼接
            var trlist = '';
            var j=0;
                for (var i in data.map){
                    var tr ="<tr>" +
                        "       <th scope='row'>"+(j+1)+"</th>"+
                        "       <td><span onclick='mlplay("+data.list[j].mid+")'>"+data.map[i][0]+"</span></td>" +
                        "       <td>"+data.map[i][1]+"</td>" +
                        "       <td>" +
                        "           <a href='javascript:favorite("+data.list[j].mid+");'>" +
                        "               <i class='mdi mdi-plus' style='border: 1px solid #ccc;padding: 2px;'></i>" +
                        "           </a>" +
                        "       </td>" +
                        "</tr>"
                    //
                    trlist += tr;
                    j++;
            }
            //2.3 将liList添加到ul中
            $list.html(trlist);
            //4.处理页面 前5后4
            //4.1 开始页数 = 当前页数-5
            var start = data.currentPage-5;
            //4.2结束页数=当前页数+4
            var end = data.currentPage+4;
            //4.3判断前面不足5个的情况
            if (start<1){
                start=1;
                end=10;
            }
            //4.4判断后面不足4的情况
            if (end>data.totalPage){
                end = data.totalPage;
                start = end-9;
            }
            //4.5总页数不足10页
            if (data.totalPage<10){
                start = 1;
                end = data.totalPage;
            }
            //4.6拼接 ul -li
            $pageUl = $("#pageUl");
            //首页  cid = 3
            var pageList = '<li><a href="javaScript:load()">首页</a></li>';
            //上一页
            var prePage = data.currentPage - 1;
            if(prePage < 1){
                prePage = 1;
            }
            pageList += '<li>' +
                '<a href="javaScript:load('+prePage+')">上一页</a></li>';
            //拼接 页码 从start开始 end 结束
            for(var i = start ; i <= end; i++){
                if(i === data.currentPage){
                    pageList += '<li onclick="load('+i+')" class="active"><a href="javaScript:">'+i+'</a></li>';
                }else {
                    pageList += '<li onclick="load('+i+')" ><a href="javaScript:">'+i+'</a></li>';
                }
            }
            //下一页
            var nextPage = data.currentPage + 1;
            if (nextPage > data.totalPage){
                nextPage = data.totalPage;
            }
            pageList += '<li onclick="load('+nextPage+')" ><a href="javascript:;">下一页</a></li>';
            //末页
            pageList += '<li onclick="load('+data.totalPage+')" ><a href="javascript:;">末页</a></li>';
            //4.7 将pageList添加到ul中
            $pageUl.html(pageList);
        }
    },"json");
}
//收藏
function favorite(mid) {
    var mlid = getParameter("mlid");
    $.post("/music/favorite/add",{mid:mid},function (data) {
        if (data=="用户未登录！"){
            alert(data);
            location.href = "userlogin.html"
        }else {
            alert("收藏成功！")
            location.href = "musicInList.html?mlid="+mlid;
        }
    })
}
function mlplay(mid) {
    $.post(
        "/music/music/findMusicsByMid",{mid:mid},function (data){
            clickmusicplay_pause();
            console.log("../"+data.musicpath)
            $("#musicaudio").attr("src","../"+data.musicpath);
            $("#musicnameaudio").html(data.musicname);
        }
    )


}
function clickmusicplay_pause(){
    $('#audioPlay').attr('src', '../images/pause2.png');
    // 开始播放当前点击的音频
    audio.load();
    audio.play();

    audio.oncanplay=function (){
        $('#totalTime').html(transTime(audio.duration))
    }


}